<?php 
/*
* StiPHPcms, A web Content management System made with PHP/MySQL
* JordSti : jord52@gmail.com
* Version : 0.0.2
*
* Copyright (C) 2009  jord52@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

class Module {
	private $cms;
	
	public $name;
	public $version;
	public $author;

	private $eventType;
	
	public function __construct($cms)
	{
		$this->name = "RaidPlanner (STI)";
		$this->version = "0.0.1";
		$this->author = "StiTeam";
		$this->eventType = array(
		'Raid 10',
		'Raid 25',
		'PvP (Bg)',
		'PvP (World Raid)',
		'Guild Event',
		'Other',
		);
		
		$this->cms = $cms;
		
	}
	
	public function getLang($key)
	{
		return $this->cms->lang[$key];
	}
	
	private function EventTypeBox()
	{
		$html = '<select name="EventType">';
		
		foreach($this->eventType as $id => $name)
		{
			$html .= '<option value="'.$id.'">'.$name.'</option>';
		}
		
		$html .= '</select>';
		
		return $html;
	}
	
	public function run()
	{
		if(isset($_GET['t']))
		{
			$task = $_GET['t'];
		}
		else
		{
			$task = 'default';
		}
		
		if($task=='newevent')
		{
			$this->EventForm();
		}
		else if($task=='viewevent')
		{
			$this->ViewEvent();
		}
		else if($task=='postevent')
		{
			$this->SaveEvent();
		}
		else if($task=='default')
		{
			$this->ShowComingEvent();
		}
		else if($task=='signup')
		{
			$this->LinkedToonList();
		}
		else if($task=='signchar')
		{
			$this->SignWithChar();
		}
		else if($task=='remchar')
		{
			$this->removeSign();
		}
	}
	
	private function removeSign()
	{
		if(isset($_GET['cid']))
		{
			$char_id = intval($_GET['cid']);
		}
		else
		{
			$this->ShowComingEvent();
			exit;
		}
		
		if(isset($_GET['eid']))
		{
			$event_id = intval($_GET['eid']);
		}
		else
		{
			$this->ShowComingEvent();
			exit;
		}
		
		$this->cms->sql_query("DELETE FROM %prefix%raidsignup WHERE event_id=$event_id AND member_id=$char_id");
		
		header('location: mod.php?mod=raidplanner&t=viewevent&eid='.$event_id);

	}
	
	private function SignWithChar()
	{
		if(isset($_GET['cid']))
		{
			$char_id = intval($_GET['cid']);
		}
		else
		{
			$this->ShowComingEvent();
			exit;
		}
		
		if(isset($_GET['eid']))
		{
			$event_id = intval($_GET['eid']);
		}
		else
		{
			$this->ShowComingEvent();
			exit;
		}
		
		$dt = $this->cms->sql_query("SELECT * FROM %prefix%raidsignup WHERE event_id=$event_id AND member_id=$char_id");
		
		if(mysql_num_rows($dt)==0)
		{
			$user_id = $this->cms->getUserId();
			$now = time();
			$this->cms->sql_query("INSERT INTO %prefix%raidsignup VALUES('',$event_id,$char_id,'$user_id',$now)");
		}
			
		header('location: mod.php?mod=raidplanner&t=viewevent&eid='.$event_id);
		
	}
	
	private function LinkedToonList()
	{
		if(isset($_GET['eid']))
		{
			$event_id = intval($_GET['eid']);
		}
		else
		{
			$this->ShowComingEvent();
			exit;
		}
		
		$this->NeedMemberRank();
		
		$user_id = $_SESSION['user_id'];
		
		$dt = $this->cms->sql_query("SELECT * FROM %prefix%members WHERE uid=$user_id");
		
		if(mysql_num_rows($dt)==0)
		{
			$html = $this->getLang('raidplanner_nolinkedchar');
		}
		else
		{
			$html = '<table width="80%">
			<tr class="rowhead">
			<td>'.$this->getLang("wow_char_name").'</td>
			<td>-</td>
			</tr>';
			$i = 0;
			while($data=mysql_fetch_array($dt))
			{
				if($i==0)
				{
					$row = 'row01';
					$i = 1;
				}
				else
				{
					$row = 'row02';
					$i = 0;
				}
				
				$html .= '<tr class="'.$row.'">
				<td>'.$data['cname'].'</td>
				<td><a href="mod.php?mod=raidplanner&t=signchar&cid='.$data['id'].'&eid='.$event_id.'">'.$this->getLang('raidplanner_sign').'</a></td>
				</tr>';
			}
			
			$html .= '</table><br><br>';
		}
		
		$this->cms->renderHeader($this->getLang('raidplanner_signup'));
		$this->cms->renderMenu();
		$this->cms->addFrame('',$html);
	}
	
	private function ViewEvent()
	{
		if(isset($_GET['eid']))
		{
			$event_id = intval($_GET['eid']);

		}
		else
		{
			$this->ShowComingEvent();
			exit;
		}
		
		require_once("core/sticode.php");
		
		$dt = $this->cms->sql_query("SELECT * FROM %prefix%raidevent WHERE id=$event_id");
		$data = mysql_fetch_array($dt);
		
		$desc = parseStiCode($data['description']);
		$day = date('d/m/Y',$data['start']);
		$start = date('H:i',$data['start']);
		$end = date('H:i',$data['end']);
		
		$typeId = $data['type'];
		
		$html = '<div align="center"><h2>'.$data['event'].'<h2>';
		$html .= '<h4>'.$this->eventType[$typeId].'</h4>';
		
		$html .= '<p>'.$day.'<br><strong>'.$this->getLang('raidplanner_start').'</strong> : '.$start.'<br><strong>'.$this->getLang('raidplanner_end').'</strong> : '.$end.'</p>';
		
		$html .= '<p>'.$desc.'</p>';
		
		$dt = $this->cms->sql_query("SELECT m.id,m.cname,m.lvl,m.achpoints,m.class,m.sex,m.race,m.rank,m.uid,m.lastup FROM %prefix%raidsignup s JOIN %prefix%members m ON(s.member_id=m.id) WHERE event_id=$event_id ORDER BY stamp");
		$nb = mysql_num_rows($dt);
		
		$html .= '<p>'.$nb.' '.$this->getLang('raidplanner_signup').'</p>';
		
		if($this->isMemberRank())
		{
			$html .= '<p><a href="mod.php?mod=raidplanner&t=signup&eid='.$data['id'].'">'.$this->getLang('raidplanner_sign').'</a></p>';
		}
		
		$html .= $this->CharTableHeader();
		
		$i = 0;
		
		while($datatoon=mysql_fetch_array($dt))
		{
			$toon = new WowToon($datatoon);
			
			if($i==0)
			{
				$class = 'row01';
				$i = 1;
			}
			else
			{
				$class = 'row02';
				$i = 0;
			}
			
			
			if($datatoon['uid']==$this->cms->getUserId())
			{
				$html .= $this->CharRowLink($class,$toon,$event_id);
			}
			else
			{
				$html .= $this->CharRow($class,$toon);
			}
			

		}
		
		$html .= '</table><br><br></div>';
		
		$this->cms->renderHeader($data['event']);
		$this->cms->renderMenu();
		$this->cms->addFrame('',$html);
	}
	
	private function ShowComingEvent()
	{
		$now = time();
		
		$html = '';
		
		if($this->isAdminRank())
		{
			$html .= '<p><a href="mod.php?mod=raidplanner&t=newevent">'.$this->getLang('raidplanner_eventform').'</a></p>';
		}
		
		$dt = $this->cms->sql_query("SELECT raid.event,raid.type,raid.start,raid.end,raid.id,COUNT(signup.id) signcount FROM %prefix%raidevent raid LEFT JOIN %prefix%raidsignup signup ON(raid.id=signup.event_id) WHERE raid.end>$now GROUP BY raid.id ORDER BY start LIMIT 10");
		
		$html .= '<table width="80%">
		<tr class="rowhead">
		<td>'.$this->getLang('raidplanner_eventname').'</td>
		<td>'.$this->getLang('raidplanner_eventtype').'</td>
		<td>-</td>
		<td>'.$this->getLang('raidplanner_start').'</td>
		<td>'.$this->getLang('raidplanner_end').'</td>
		<td>'.$this->getLang('raidplanner_signup').'</td>
		</tr>';
		
		$i = 0;
		
		while($data=mysql_fetch_array($dt))
		{
			if($i==0)
			{
				$i = 1;
				$row = "row01";
			}
			else
			{
				$i = 0;
				$row = "row02";
			}
			
			$typeId = $data['type'];
			$day = date('d/m/Y',$data['start']);
			$start = date('H:i',$data['start']);
			$end = date('H:i',$data['end']);
			
			$tmp = $data['end'] - $data['start'];
			
			//echo $tmp.'<br>';
			
			$type = $this->eventType[$typeId];
			
			$html .= '<tr class="'.$row.'">
			<td><a href="mod.php?mod=raidplanner&t=viewevent&eid='.$data['id'].'">'.$data['event'].'</a></td>
			<td>'.$type.'</td>
			<td>'.$day.'</td>
			<td>'.$start.'</td>
			<td>'.$end.'</td>
			<td>'.$data['signcount'].'</td>
			</tr>';
		}
		
		$html .= '</table>';
		
		
		$this->cms->renderHeader($this->getLang('raidplanner_comingevent'));
		$this->cms->renderMenu();
		$this->cms->addFrame('',$html);
	}
	
	private function CharTableHeader()
	{

		return '<table align="center" width="80%" border="0" cellpadding="0" cellspacing="0">
            <tr class="rowhead">
              <td width="50%">'.$this->getLang("wow_char_name").'</td>
              <td width="25%">'.$this->getLang("wow_level").'</td>
              <td width="25%">&nbsp;</td>
            </tr>';

	}
	
	private function CharRow($rowclass,$toon)
	{
		return '<tr class="'.$rowclass.'">
              	<td><a href="'.$toon->getArmoryUrl($this->cms->settings['wow_realm']).'" target="_blank">'.$toon->cname.'</a></td>
              	<td>'.$toon->lvl.'</td>
              	<td><img src="'.$toon->getRaceIcon().'"><img src="'.$toon->getClassIcon().'" width="18" height="18"></td>
            	</tr>';

	}
	
	private function CharRowLink($rowclass,$toon,$event_id)
	{
		return '<tr class="'.$rowclass.'">
              	<td><a href="'.$toon->getArmoryUrl($this->cms->settings['wow_realm']).'" target="_blank">'.$toon->cname.'</a> - <a href="mod.php?mod=raidplanner&t=remchar&cid='.$toon->id.'&eid='.$event_id.'">'.$this->getLang('delete').'</a></td>
              	<td>'.$toon->lvl.'</td>
              	<td><img src="'.$toon->getRaceIcon().'"><img src="'.$toon->getClassIcon().'" width="18" height="18"></td>
            	</tr>';

	}
	
	private function EventForm()
	{
		require_once("core/tools/date.php");
		require_once("core/sticode.php");
		
		$this->cms->loadStiCodeScript();
		
		$this->NeedAdminRank();
		
		$html = '<form method="post" action="mod.php?mod=raidplanner&t=postevent">
			<fieldset>
			<legend>'.$this->getLang('raidplanner_eventform').'</legend>
			<p><label for="EventName">'.$this->getLang('raidplanner_eventname').' : </label><input type="text" name="EventName" id="cname" tabindex="10" /></p>
			<p><label for="EventType">'.$this->getLang('raidplanner_eventtype').' : </label>'.$this->EventTypeBox().'</p>
			<p><label for="DateStart">'.$this->getLang('raidplanner_start').' : </label>'.dateFieldWithTime(time(),'start_').'</p>
			<p><label for="DateEnd">'.$this->getLang('raidplanner_end').' : </label>'.dateFieldWithTime(time(),'end_').'</p>
			<p>'.$this->getLang('raidplanner_desc').'<br>'.StiCodeHTMLFormBar('description').'<textarea name="description" id="description" cols="60" rows="20"></textarea></p>
			<p><input type="submit" value="'.$this->getLang('save').'"/></p>
			</fieldset>
			</form>';
		
		$this->cms->renderHeader($this->getLang('raidplanner_eventform'));
		$this->cms->renderMenu();
		$this->cms->addFrame('',$html);
	}
	
	private function SaveEvent()
	{
		$this->NeedAdminRank();
		
		require_once("core/tools/string.php");
		require_once("core/tools/date.php");
		
		$data = array_sql_escape($_POST);
		$data = array_htmlentities($data);
		
		$start = getDateField('start_');
		$end = getDateField('end_');
		
		$name = $data['EventName'];
		$type = intval($data['EventType']);
		$desc = $data['description'];
		$user_id = $_SESSION['user_id'];
		$description = $data['description'];
		$stamp = time();
		$this->cms->sql_query("INSERT INTO %prefix%raidevent VALUES('','$name','$description',$start,$end,$type,$user_id,$stamp)");
		
		$html = '<h1>'.$this->getLang('raidplanner_eventposted').'</h1>';
		
		$this->cms->renderHeader($this->getLang('raidplanner_eventform'));
		$this->cms->renderMenu();
		$this->cms->addFrame('',$html);
	}
	
	public function install()
	{
		//Empty
	}
	
	private function NeedMemberRank()
	{
		if($this->cms->getUserRank()<$this->cms->settings['wow_memberrank'])
		{
			$this->cms->error($this->getLang('right_error'));
		}
	}
	
	private function isMemberRank()
	{
		if($this->cms->getUserRank()<$this->cms->settings['wow_memberrank'])
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	private function NeedAdminRank()
	{
		if($this->cms->getUserRank()<$this->cms->settings['wow_rankadmin'])
		{
			$this->cms->error($this->getLang('right_error'));
		}
	}
	
	private function isAdminRank()
	{
		if($this->cms->getUserRank()<$this->cms->settings['wow_rankadmin'])
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}
class WowToon{
	public $lvl;
	public $cname;
	public $achpoints;
	public $rank;
	public $pclass;
	public $race;
	public $sex;
	public $id;
	public $uid;

	public function __construct($data)
	{
		$this->lvl = $data['lvl'];
		$this->cname = $data['cname'];
		$this->achpoints = $data['achpoints'];
		$this->rank = $data['rank'];
		$this->pclass = $data['class'];
		$this->race = $data['race'];
		$this->sex = $data['sex'];
		$this->id = $data['id'];
		$this->uid = $data['uid'];
	}

	public function getArmoryUrl($guild_name)
	{
		$url = 'http://www.wowarmory.com/character-sheet.xml?r='.str_replace(' ','+',$guild_name).'&n='.$this->cname;
		return $url;
	}
	
	public function getRaceIcon()
	{
		$imgurl = 'images/wowarmory/race/'.strtolower(str_replace(' ','',$this->race));
		if($this->sex == 0)
		{
			$imgurl .= '_m';
		}
		else
		{
			$imgurl .= '_f';
		}

		$imgurl .= '.gif';
		return $imgurl;
	}

	public function getClassIcon()
	{
		$imgclass = 'images/wowarmory/class/'.strtolower(str_replace(' ','',$this->pclass)).'.png';
		return $imgclass;
	}

}
?>